package br.com.dubs.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.dubs.core.ejb.ClientHandler;
import br.com.dubs.core.interfaces.ClientHandlerInterface;
import br.com.dubs.persistence.entities.ClientEntity;
import br.com.dubs.persistence.entities.MsgEntity;
import br.com.dubs.persistence.exceptions.GenericDAOException;

/**
 * Servlet implementation class PopulateServlet
 */
@WebServlet("/PopulateServlet")
public class PopulateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private ClientHandlerInterface cHandler;

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try {
			ClientEntity client = new ClientEntity();
			ClientEntity client2 = null;
			client.setLogin("bazinga");
			client.setPassword("bazinga");
			client.setName("Adamastor");
			client.setEmail("jokes@gmail.com");
			MsgEntity msg = new MsgEntity();
			msg.setDate(new Date());
			msg.setMsg("Teste 123");
			msg.setTitulo("Teste");
			msg.setReceiver(client);
			msg.setSender(client);
			client.getReceivedMsg().add(msg);
			cHandler.addClient(client);

			// test Login

			client2 = cHandler.login("bazinga", "bazinga");
			out.print(client2.getName());
		} finally {
			out.close();
		}
	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}

}
